/**
 * 
 */
package menus.demm;

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.geom.Point2D;

import menus.common.AbstractItem;
import menus.common.AbstractMenu;
import fr.lri.swingstates.canvas.CEllipse;
import fr.lri.swingstates.canvas.CPolyLine;

/**
 * A DEMM Item
 * 
 * @author mathieu
 */
public class DEMMItem extends AbstractItem {
	
	protected double sliceAngle, startAngle, startRadius, endRadius;
	protected int level;
	
	
	
	// ----------------------------------------------------------------- XXX

	
	
	/**
	 * @param menu
	 */
	public DEMMItem(AbstractMenu menu, int level, String label, double sliceAngle, double startAngle, double startRadius, double endRadius, String tag) {
		
		super(menu, label, tag);
		
		this.level = level;
		
		this.sliceAngle = sliceAngle;
		this.startAngle = startAngle;
		this.startRadius = startRadius;
		this.endRadius = endRadius;
		
	}

	/**
	 * Generates a slice shape, without adding it to the canvas or the menu.
	 * Can be used for generating the menu or the ghosts.
	 * 
	 * @return
	 */
	@Override
	protected CPolyLine createShape(){
		
		CPolyLine shape = new CPolyLine(0, 0);
		shape.lineTo(endRadius, 0)
			.arcTo(0, -sliceAngle, endRadius, endRadius)
			.lineTo(0, 0)
			
			.setReferencePoint(0, 0)
			.rotateBy(-startAngle - sliceAngle/2);
		
		CPolyLine resultSlice = shape.getSubtraction(new CEllipse(-startRadius-4, -startRadius-4, 2*startRadius+8, 2*startRadius+8));
		
		resultSlice.setAntialiased(true)
			.setOutlined(true)
			.setStroke(new BasicStroke(1))
			.setOutlinePaint(Color.black)
			.setFilled(true)
			.setFillPaint(Color.red)
			
			.addTag(tag)
			
			.setDrawable(false)
			.setPickable(false);
		
		return resultSlice;
	}
	
	protected CPolyLine[] createIdealMarks(){
		
		double[] frontiers = ((DEMM)menu).getFrontiers();
		
		CPolyLine[] idealMarks = new CPolyLine[level+1];
		
		for (int frontier = 0; frontier < level; frontier++){
			
			CPolyLine idealMark = menu.getCanvas().newPolyLine(0, 0);
			
			idealMark.lineTo(frontiers[frontier+1] - frontiers[frontier], 0)
					.setFilled(false)
					.setOutlinePaint(((DEMM)menu).getColors()[frontier])
					.setStroke(((DEMMStateMachine)menu.getStateMachine()).getMark().getStroke())
					
					.setReferencePoint(0, 0)
					.translateTo(frontiers[frontier]*Math.cos(startAngle), -frontiers[frontier]*Math.sin(startAngle))
					.rotateTo(-startAngle)
					
					.addTag(tag)
					
					.setDrawable(true)
					.setPickable(false);
			
			idealMarks[frontier] = idealMark;
			
		}
		
		// Last one
		
		CPolyLine markEnd = menu.getCanvas().newPolyLine(0, 0);
		
		markEnd.lineTo( (frontiers[level+1] - frontiers[level]) / 2, 0)
				.setFilled(false)
				.setOutlinePaint(((DEMM)menu).getColors()[level])
				.setStroke(((DEMMStateMachine)menu.getStateMachine()).getMark().getStroke())
				
				.setReferencePoint(0, 0)
				.translateTo(frontiers[level]*Math.cos(startAngle), -frontiers[level]*Math.sin(startAngle))
				.rotateTo(-startAngle)
				
				.addTag(tag)
				
				.setDrawable(true)
				.setPickable(false);
		
		idealMarks[level] = markEnd;
		
		return idealMarks;
		
	}
	
	@Override
	public void ghost(Point2D where) {
		//super.ghost(where);
		CPolyLine[] idealMarks = createIdealMarks();
		for(CPolyLine m : idealMarks)
			super.ghost(where, m);
	}

}
